﻿package controllers 
{
	import events.QueryErrorEvent;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.net.URLLoader;
	
	/**
	 * ...
	 * @author Slavshik Alexander
	 */
	public class BaseController extends EventDispatcher
	{
		[Event(name="ianswer", type="events.QueryErrorEvent")]
		[Event(name="serror", type="events.QueryErrorEvent")]
		[Event(name="nphotos", type="events.QueryErrorEvent")]
		[Event(name="ioError", type="flash.events.IOErrorEvent")]
		
		protected var urlLoader:URLLoader = new URLLoader();
		
		public function BaseController() {
			urlLoader.addEventListener(IOErrorEvent.IO_ERROR, onIOError);
		}
		private function onIOError(e:IOErrorEvent):void {
			dispatchEvent(new IOErrorEvent(IOErrorEvent.IO_ERROR));
		}
		public function stopQueries():void {
			try { urlLoader.close(); } catch (err:Error) { }
		}
		protected function checkXML(data:*):Boolean{
			var xmlData:XML;
			try { 
				xmlData = new XML(data);
			} catch (err:Error) { 
				trace("Invalid server answer");
				return false;
			}
			if (int(xmlData.error_code)) {
				trace(xmlData.error_msg);
				return false;
			}
			return true;
		}
		
	}
	
}